//
//  BankViewController.m
//  LiquorChain
//
//  Created by apple on 2019/1/3.
//  Copyright © 2019年 王冬. All rights reserved.
//

#import "BankViewController.h"
#import "AddBankController.h"
@interface BankViewController ()
@property (nonatomic,strong) NSArray *dateArray;
@property (nonatomic,strong) UIButton *addBankBtn;
@end

@implementation BankViewController

-(NSArray *)dateArray{
    if (!_dateArray) {
        _dateArray =[NSArray array];
    }
    return _dateArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title =@"银行卡";
    self.dateArray = @[@"1"];
    self.view.backgroundColor = BACKGROUNDCOLOR;
    if (self.dateArray.count ==0) {
        UIView *headView =[[UIView alloc]init];
        [self.view addSubview:headView];
        headView.backgroundColor =[UIColor whiteColor];
        [headView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(kNavBarHeight +15);
            make.height.mas_equalTo(SCREEN_HEIGHT-kNavBarHeight - 15 -kIPhoneXBottomHeight);
        }];
        UIImageView *headImageView =[[UIImageView alloc]init];
        [headView addSubview:headImageView];
        headImageView.image = [UIImage imageNamed:@"nobank"];
        [headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(50);
           // make.left.mas_equalTo(115);
            make.centerX.mas_equalTo(headView.mas_centerX);
            //make.right.mas_equalTo(-115);
            make.width.mas_equalTo(150);
            make.height.mas_equalTo(80);
        }];
        UILabel *infoLabel =[[UILabel alloc]init];
        [headView addSubview:infoLabel];
        infoLabel.numberOfLines = 2;
        infoLabel.text = @"添加银行卡方便资产管理享受交易服务";
        infoLabel.font =[UIFont systemFontOfSize:15];
        infoLabel.textColor =[UIColor lightGrayColor];
        infoLabel.textAlignment = NSTextAlignmentCenter;
        [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(headImageView.mas_bottom).with.offset(30);
            make.centerX.mas_equalTo(headView.mas_centerX);
            make.width.mas_equalTo(164);
            make.height.mas_equalTo(38);
            
        }];
        
        self.addBankBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        [headView  addSubview:self.addBankBtn];
        [self.addBankBtn setTitle:@"添加银行卡" forState:UIControlStateNormal];
        [self.addBankBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.addBankBtn.backgroundColor = CUSTOMCOLOR;
        self.addBankBtn.layer.cornerRadius = 5;
        [self.addBankBtn addTarget:self action:@selector(addBankClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.addBankBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.top.mas_equalTo(infoLabel.mas_bottom).with.offset(60);
            make.height.mas_equalTo(50);
        }];
    }else{
        self.view.backgroundColor =[UIColor whiteColor];
        UIView *bankView =[[UIView alloc]init];
        bankView.layer.cornerRadius = 5;
        bankView.backgroundColor = CUSTOMCOLOR;
        [self.view addSubview:bankView];
        [bankView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(kNavBarHeight +20);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(142);
        }];
        
        UILabel *bankNameLabel =[[UILabel alloc]init];
        [bankView addSubview:bankNameLabel];
        bankNameLabel.text = @"建设银行";
        bankNameLabel.textColor =[UIColor whiteColor];
        bankNameLabel.font =[UIFont systemFontOfSize:20];
        [bankNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_equalTo(20);
        }];
        UILabel *bankClassLabel =[[UILabel alloc]init];
        [bankView addSubview:bankClassLabel];
        bankClassLabel.text = @"储蓄卡";
        bankClassLabel.textColor =[UIColor whiteColor];
        bankClassLabel.font =[UIFont systemFontOfSize:14];
        [bankClassLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.top.mas_equalTo(bankNameLabel.mas_bottom).with.offset(10);
            make.height.mas_equalTo(13);
        }];
        UILabel *bankNumberLabel =[[UILabel alloc]init];
        [bankView addSubview:bankNumberLabel];
        NSString *bankStr =[NSString stringWithFormat:@"6225014312346885"];
        bankNumberLabel.textColor =[UIColor whiteColor];
        bankNumberLabel.font =[UIFont systemFontOfSize:26];
        bankNumberLabel.text = [self getNewBankNumWitOldBankNum:bankStr];
        [bankNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(bankClassLabel.mas_bottom).with.offset(30);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_equalTo(20);
        }];
        
        
        UILabel *tiLabel =[[UILabel alloc]init];
        [self.view addSubview:tiLabel];
        tiLabel.text= @"温馨提示";
        [tiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(bankView.mas_bottom).with.offset(40);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(20);
        }];
        
        
        UILabel  *bottomLabel =[[UILabel alloc]init];
        bottomLabel.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:bottomLabel];
        bottomLabel.font =[UIFont systemFontOfSize:15];
        bottomLabel.textColor =[UIColor grayColor];
        [bottomLabel setValue:@(30) forKey:@"lineSpacing"];
        NSString *warnStr =[NSString stringWithFormat:@"只能绑定一张储蓄卡,\n绑定后可以使用该卡进行交易,\n若有疑问，请拨打客服热线电话:400-888-8888"];
        bottomLabel.text = warnStr;
        bottomLabel.numberOfLines = 0;
        [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(tiLabel.mas_bottom).with.offset(0);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(100);
        }];
        
    }
  
}
-(void)addBankClick:(UIButton *)sender{
    NSLog(@"添加r银行卡");
    AddBankController *addVC =[[AddBankController alloc]init];
    [self.navigationController pushViewController:addVC animated:YES];
}

-(NSString *)getNewBankNumWitOldBankNum:(NSString *)bankNum
{
    NSMutableString *mutableStr;
    if (bankNum.length) {
        mutableStr = [NSMutableString stringWithString:bankNum];
        for (int i = 0 ; i < mutableStr.length; i ++) {
            if (i>3&&i<mutableStr.length - 4) {
                [mutableStr replaceCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
            }
        }
        NSString *text = mutableStr;
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        text = [text stringByReplacingOccurrencesOfString:@"   " withString:@""];
        NSString *newString = @"";
        while (text.length > 0) {
            NSString *subString = [text substringToIndex:MIN(text.length, 4)];
            newString = [newString stringByAppendingString:subString];
            if (subString.length == 4) {
                newString = [newString stringByAppendingString:@"   "];
            }
            text = [text substringFromIndex:MIN(text.length, 4)];
        }
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
        return newString;
    }
    return bankNum;
    
}

@end
